<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
      <el-form-item :label="$t('CoalInvitation.userInfoId')" prop="userInfoId" >
        <el-input :placeholder="t('CoalInvitation.inputUserInfoIdTip')" v-model="state.queryForm.userInfoId"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.createUserName')" prop="createUserName" >
        <el-input :placeholder="t('CoalInvitation.inputCreateUserNameTip')" v-model="state.queryForm.createUserName"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.createUserPhoto')" prop="createUserPhoto" >
        <el-input :placeholder="t('CoalInvitation.inputCreateUserPhotoTip')" v-model="state.queryForm.createUserPhoto"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.invitationText')" prop="invitationText" >
        <el-input :placeholder="t('CoalInvitation.inputInvitationTextTip')" v-model="state.queryForm.invitationText"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.invitationTitle')" prop="invitationTitle" >
        <el-input :placeholder="t('CoalInvitation.inputInvitationTitleTip')" v-model="state.queryForm.invitationTitle"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.invitationType')" prop="invitationType" >
        <el-input :placeholder="t('CoalInvitation.inputInvitationTypeTip')" v-model="state.queryForm.invitationType"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.isTop')" prop="isTop" >
        <el-input :placeholder="t('CoalInvitation.inputIsTopTip')" v-model="state.queryForm.isTop"
          style="max-width: 180px" />
      </el-form-item>
	    <el-form-item :label="t('CoalInvitation.topStartDate')" prop="topStartDate">
            <el-date-picker type="datetime" :placeholder="t('CoalInvitation.inputTopStartDateTip')" v-model="state.queryForm.topStartDate"></el-date-picker>
	    </el-form-item>
      </el-col>
      <el-form-item :label="$t('CoalInvitation.likesCount')" prop="likesCount" >
        <el-input :placeholder="t('CoalInvitation.inputLikesCountTip')" v-model="state.queryForm.likesCount"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.collectCount')" prop="collectCount" >
        <el-input :placeholder="t('CoalInvitation.inputCollectCountTip')" v-model="state.queryForm.collectCount"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.browseCount')" prop="browseCount" >
        <el-input :placeholder="t('CoalInvitation.inputBrowseCountTip')" v-model="state.queryForm.browseCount"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.contactPhone')" prop="contactPhone" >
        <el-input :placeholder="t('CoalInvitation.inputContactPhoneTip')" v-model="state.queryForm.contactPhone"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.state')" prop="state" >
        <el-input :placeholder="t('CoalInvitation.inputStateTip')" v-model="state.queryForm.state"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.createDate')" prop="createDate" >
        <el-input :placeholder="t('CoalInvitation.inputCreateDateTip')" v-model="state.queryForm.createDate"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.invitationLabel')" prop="invitationLabel" >
        <el-input :placeholder="t('CoalInvitation.inputInvitationLabelTip')" v-model="state.queryForm.invitationLabel"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalInvitation.releaseDate')" prop="releaseDate" >
        <el-input :placeholder="t('CoalInvitation.inputReleaseDateTip')" v-model="state.queryForm.releaseDate"
          style="max-width: 180px" />
      </el-form-item>
          <el-form-item>
            <el-button formDialogRef icon="search" type="primary" @click="getDataList">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button icon="Refresh" formDialogRef @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button formDialogRef icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'coalSystem_CoalInvitation_add'">
            {{ $t('common.addBtn') }}
          </el-button>
          <el-button plain formDialogRef :disabled="multiple" icon="Delete" type="primary" class="ml10"
            v-auth="'coalSystem_CoalInvitation_del'" @click="handleDelete(selectObjs)">
            {{ $t('common.delBtn') }}
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" 	:export="'coalSystem_CoalInvitation_export'"
						@exportExcel="exportExcel" class="ml10" style="float: right;margin-right: 20px"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" style="width: 100%"
        @selection-change="handleSelectionChange" @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" :label="t('CoalInvitation.index')" width="40" />
          <el-table-column prop="userInfoId" :label="t('CoalInvitation.userInfoId')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="createUserName" :label="t('CoalInvitation.createUserName')"  show-overflow-tooltip/>
          <el-table-column prop="createUserPhoto" :label="t('CoalInvitation.createUserPhoto')"  show-overflow-tooltip/>
          <el-table-column prop="invitationText" :label="t('CoalInvitation.invitationText')"  show-overflow-tooltip/>
          <el-table-column prop="invitationTitle" :label="t('CoalInvitation.invitationTitle')"  show-overflow-tooltip/>
          <el-table-column prop="invitationType" :label="t('CoalInvitation.invitationType')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="isTop" :label="t('CoalInvitation.isTop')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="topStartDate" :label="t('CoalInvitation.topStartDate')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="likesCount" :label="t('CoalInvitation.likesCount')"  show-overflow-tooltip/>
          <el-table-column prop="collectCount" :label="t('CoalInvitation.collectCount')"  show-overflow-tooltip/>
          <el-table-column prop="browseCount" :label="t('CoalInvitation.browseCount')"  show-overflow-tooltip/>
          <el-table-column prop="contactPhone" :label="t('CoalInvitation.contactPhone')"  show-overflow-tooltip/>
          <el-table-column prop="state" :label="t('CoalInvitation.state')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="createDate" :label="t('CoalInvitation.createDate')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="invitationLabel" :label="t('CoalInvitation.invitationLabel')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="releaseDate" :label="t('CoalInvitation.releaseDate')" sortable="custom" show-overflow-tooltip/>
        <el-table-column :label="$t('common.action')" width="150">
          <template #default="scope">
            <el-button icon="edit-pen" text type="primary" v-auth="'coalSystem_CoalInvitation_edit'"
              @click="formDialogRef.openDialog(scope.row.invitationId)">{{ $t('common.editBtn') }}</el-button>
            <el-button icon="delete" text type="primary" v-auth="'coalSystem_CoalInvitation_del'" @click="handleDelete([scope.row.invitationId])">{{
              $t('common.delBtn')
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
  </div>
</template>

<script setup lang="ts" name="systemCoalInvitation">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs } from "/@/api/coalSystem/CoalInvitation";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';
import { useI18n } from "vue-i18n";

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const { t } = useI18n()
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/coalSystem/CoalInvitation/export', state.queryForm, 'CoalInvitation.xlsx')
}

// 多选事件
const handleSelectionChange = (objs: any) => {
	selectObjs.value.push(...objs.map((val: any) => val.invitationId));
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>